package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ViewAppointmentService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentDetailVo;
import com.atguigu.lease.web.app.vo.appointment.AppointmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment>
        implements ViewAppointmentService {

    @Autowired
    private ViewAppointmentMapper viewAppointmentMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    // 查询个人预约看房列表
    public List<AppointmentItemVo> listItem(Long userId) {
        return viewAppointmentMapper.list(userId);
    }

    @Override
    public AppointmentDetailVo getDetailById(Long id) {

        //1.根据id获取预约基本信息
        ViewAppointment viewAppointment = viewAppointmentMapper.selectById(id);

        //2.获取公寓id，根据公寓id获取公寓的详细信息
        Long apartmentId = viewAppointment.getApartmentId();

        //根据公寓id查询公寓基本信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(apartmentId);
        //获取这个公寓的标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.listLabelInfoByApartmentId(apartmentInfo.getId());

        //获取这个公寓的图片列表
        List<GraphVo> graphVoList = graphInfoMapper.list(apartmentInfo.getId());


        //计算这个公寓所有房间中最低租金
        List<RoomInfo> roomInfoList = roomInfoMapper.listByApartmentId(apartmentInfo.getId());
        BigDecimal minRent = roomInfoList.stream().map(RoomInfo::getRent).min(BigDecimal::compareTo).get();

        //ApartmentDetailVo
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setGraphVoList(graphVoList);
        apartmentItemVo.setMinRent(minRent);

        //封装结果
        AppointmentDetailVo appointmentDetailVo = new AppointmentDetailVo();
        BeanUtils.copyProperties(viewAppointment, appointmentDetailVo);
        appointmentDetailVo.setApartmentItemVo(apartmentItemVo);
        return appointmentDetailVo;
    }
}




